<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="时间" prop="timestamp">
                    <daterange-picker
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>

                <el-form-item label="员工ID" prop="user_id">
                    <ds-select class="w-[280px]"
                               v-model="queryParams.user_id"
                               clearable
                               placeholder="请选择员工ID"
                               :apiRelationDatas="apiAaRecordRelationDatas"
                               field='user_id'
                               show_field="real_name"
                    />
                </el-form-item>
                <el-form-item label="次数" prop="count">
                    <el-input class="w-[280px]" v-model="queryParams.count" clearable placeholder="请输入次数" />
                </el-form-item>
                <el-form-item label="单号" prop="order_number">
                    <el-input class="w-[280px]" v-model="queryParams.order_number" clearable placeholder="请输入单号" />
                </el-form-item>
                <el-form-item label="类别ID" prop="category_id">
                    <ds-select class="w-[280px]"
                               v-model="queryParams.category_id"
                               clearable
                               placeholder="请选择类别ID"
                               :apiRelationDatas="apiAaRecordRelationDatas"
                               field='category_id'
                               show_field="category_name"
                    />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select class="w-[280px]" v-model="queryParams.status" clearable placeholder="请选择状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="(item, index) in dictData.qrcode"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage();analysisSelectData.getAnalysisSelectData()">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['aa_record/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button
                v-perms="['aa_record/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button>
            <el-button
                type="primary"
                v-perms="['aa_record/delete']"
                :disabled="!pager.lists.length"
                @click="updateAllState.handleUpdateAllState"
            >
                批量修改
            </el-button>
            <div class="mt-4">
<!--                [-->
<!--                {-->
<!--                "category_id": 1,-->
<!--                "count": 41,-->
<!--                "category_name": "测试"-->
<!--                },-->
<!--                {-->
<!--                "category_id": 2,-->
<!--                "count": 1,-->
<!--                "category_name": "测试2"-->
<!--                }-->
<!--                ]-->
<!--                渲染-->

                <el-table :data="analysisSelectData.list" >
                    <el-table-column label="类别" prop="category_name"/>
                    <el-table-column label="次数" prop="count" show-overflow-tooltip />
                </el-table>


                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="编号" prop="id" show-overflow-tooltip />
                    <el-table-column label="时间" prop="timestamp" show-overflow-tooltip />
                    <el-table-column label="员工ID" prop="user_id">
                        <template #default="{ row }">
                            {{ relationData.user_id[row.user_id] }}
                        </template>
                    </el-table-column>
                    <el-table-column label="次数" prop="count" show-overflow-tooltip />
                    <el-table-column label="单号" prop="order_number" show-overflow-tooltip />
                    <el-table-column label="类别ID" prop="category_id">
                        <template #default="{ row }">
                            {{ relationData.category_id[row.category_id] }}
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status">
                        <template #default="{ row }">
                            <dict-value :options="dictData.qrcode" :value="row.status" />
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['aa_record/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['aa_record/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="updateAllState.show = false" />
        <el-dialog v-model="updateAllState.show" title="批量修改" width="30%" @close="updateAllState.show = false">
            <el-form :model="updateAllState.formData" label-width="80px">
                <el-form-item label="类别ID" prop="new_category_id">
                    <ds-select
                        v-model="updateAllState.formData.new_category_id"
                        clearable
                        placeholder="请选择类别ID"
                        :apiRelationDatas="apiAaRecordRelationDatas"
                        field="category_id"
                        show_field="category_name"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="updateAllState.handleUpdateAllStateSubmit">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="aaRecordLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiAaRecordLists, apiAaRecordDelete, apiAaRecordRelationDatas,apiAaRecordAnalysis,apiAaRecordUpdateAll } from '@/api/aa_record'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    timestamp: '',
    user_id: '',
    count: '',
    order_number: '',
    category_id: '',
    status: '',
    start_time: '',
    end_time: ''
})

//批量修改
const updateAllState=reactive({
    show:false,
    formData:{
        new_category_id:''
    },
    handleUpdateAllState:function(){
        updateAllState.show=true
    },
    handleUpdateAllStateSubmit:function(){
        console.log(updateAllState.formData)
        apiAaRecordUpdateAll({ ...updateAllState.formData,...queryParams }).then(()=>{
            updateAllState.show=false
            getLists()
        })
    }
})


// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('qrcode')
// 数据源数据
const relationData = ref({
    user_id: [],
    category_id: []
})

const analysisSelectData = reactive({
    list: [],
    getAnalysisSelectData:function() {
        apiAaRecordAnalysis(queryParams).then((res) => {
            this.list = res
        })
    }
})
analysisSelectData.getAnalysisSelectData()

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: (params: any) => {
        return new Promise((resolv, reject) => {
            const r = apiAaRecordLists(params)
            r.then(async r => {
                relationData.value.user_id = (await apiAaRecordRelationDatas({
                    field: 'user_id',
                    show_field: 'real_name',
                    ids: Array.from(new Set(r.lists.map((x: any) => x.user_id))).join(',')
                })).rows.reduce((o: any, i: any) => {
                    o[i.id] = i.title
                    return o
                }, {})
                relationData.value.category_id = (await apiAaRecordRelationDatas({
                    field: 'category_id',
                    show_field: 'category_name',
                    ids: Array.from(new Set(r.lists.map((x: any) => x.category_id))).join(',')
                })).rows.reduce((o: any, i: any) => {
                    o[i.id] = i.title
                    return o
                }, {})
                resolv(r)
            })
            return r
        })
    }
    ,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiAaRecordDelete({ id })
    getLists()
}

getLists()
</script>

